/**
 * Copyright (C), 2018-2018, https://blog.fundodoo.com
 * Author:   醉探索戈壁
 * Date:     2018/7/1 下午6:00
 * Description: 用户逻辑实现类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.fundodoo.blog.drunk8.service.impl;

import com.fundodoo.blog.drunk8.entity.User;
import com.fundodoo.blog.drunk8.service.UserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author 醉探索戈壁
 * @create 2018/7/1 下午6:00
 * @since 1.0.0
 */
@Service
@Log4j2
public class UserServiceImpl implements UserService {

    private static final Map<Long, User> DATABASES = new HashMap<>();

    static {
        DATABASES.put(1L, new User(1L, "u1", "p1"));
        DATABASES.put(2L, new User(2L, "u2", "p2"));
        DATABASES.put(3L, new User(3L, "u3", "p3"));
    }

    @Cacheable(value = "user",key = "#user.id")
    @Override
    public User saveOrUpdate(User user) {
        DATABASES.put(user.getId(), user);
        log.info("进入 saveOrUpdate 方法");
        return user;
    }

    @Cacheable(value = "user",key = "#id")
    @Override
    public User get(Long id) {
        log.info("进入 get 方法");
        return DATABASES.get(id);
    }

    @Cacheable(value = "user",key = "#id")
    @Override
    public void delete(Long id) {
        DATABASES.remove(id);
        log.info("进入 delete 方法");
    }
}
